from django.shortcuts import render, HttpResponse, HttpResponseRedirect
from app01 import models
import time


# Create your views here.
def login(request):
    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')
        # print(username,password)
        user_obj = models.State.objects.filter(state_user=username, state_pwd=password).first()
        # print(user_obj)
        jtime = time.asctime(time.localtime(time.time()))
        if user_obj:
            print('登录成功')
            """
            如果验证通过，则设置session,并返回home.html
            """
            request.session["is_login"] = True
            # request.session["username"] = user_obj
            return render(request, 'home.html', locals())
        else:
            ret = "用户名或密码错误"
        return render(request, "statelogin.html")
    request.session["is_login"] = False
    return render(request, 'statelogin.html')


def home(request):
    is_login = request.session.get("is_login")
    print(is_login)
    # username = request.session.get("username")
    if is_login:
        print("username")
        jtime = time.asctime(time.localtime(time.time()))
        return render(request, 'home.html', locals())
    """如果没有登录则跳转至登录页面"""
    return render(request, "statelogin.html")


"""
urls.py
    path(r'room_info/',views.room_info),    #房间信息
    path(r'guid_info/',views.guid_info),    #引导员信息
    path(r'kaccount_info/',views.kaccount_info),#k歌账号
    path(r'help_info/',views.help_info),    #帮助文档
"""


def room_info(request):
    room_querys = models.Room.objects.order_by('room_state').all()
    for room_query in room_querys:
        # room_query.room_time=time.asctime( time.localtime(time.time()) )
        if room_query.room_state == "f":
            room_query.room_state = "未使用"
        else:
            room_query.room_state = "已使用"
    return render(request, "room_info.html", locals())


def guid_info(request):
    guid_querys = models.Guide.objects.order_by('guide_state').all()
    for guid_query in guid_querys:
        # room_query.room_time=time.asctime( time.localtime(time.time()) )
        if guid_query.guide_state == "f":
            guid_query.guide_state = "未使用"
        else:
            guid_query.guide_state = "已使用"
        if guid_query.guide_gender == "f":
            guid_query.guide_gender = "女生"
        else:
            guid_query.guide_gender = "男生"
    return render(request, "guid_info.html", locals())


def kaccount_info(request):
    kaccount_querys = models.Account.objects.order_by('account_state').all()
    for kaccount_query in kaccount_querys:
        # room_query.room_time=time.asctime( time.localtime(time.time()) )
        if kaccount_query.account_state == "f":
            kaccount_query.account_state = "未使用"
        else:
            kaccount_query.account_state = "已使用"
    return render(request, "kaccount_info.html", locals())


def help_info(request):
    return render(request, "help_info.html")


def work(request):
    # room_querys = models.Room.objects.order_by('room_state').all()
    # guid_querys = models.Guide.objects.order_by('guide_state').all()
    # kaccount_querys = models.Account.objects.order_by('account_state').all()
    room_querys = models.Room.objects.filter(room_state__exact='f')
    guid_querys = models.Guide.objects.filter(guide_state__exact='f')
    kaccount_querys = models.Account.objects.filter(account_state__exact='f')
    return render(request, "home_work.html", locals())

    # path(r'work_release/',views.work_release),# 提交订单
    # path(r'work_place/',views.work_place),#释放订单


# 提交订单
def work_place(request):
    if request.method == 'POST':
        # place_room = 99
        # place_account = 99
        # place_guide = 99
        place_room = request.POST.get('room_id')
        place_time = request.POST.get('room_time')
        place_account = request.POST.get('account_id')
        place_guide = request.POST.get('guide_id')
        # print(place_room,place_account,place_guide)
        if place_room and place_time and place_account and place_guide:
            import datetime
            tmie_h = float(place_time)
            time = datetime.datetime.now() + datetime.timedelta(hours=tmie_h)
            # print(time)
            models.Room.objects.filter(pk=place_room).update(room_state='t')
            models.Room.objects.filter(pk=place_room).update(room_time=time)
            models.Account.objects.filter(pk=place_account).update(account_state='t')
            guide = models.Guide.objects.get(pk=place_guide)
            guide.guide_state = 't'
            guide.guide_frequency = guide.guide_frequency + 1
            guide.save()
            # models.Guide.objects.filter(pk=place_guide).update(guide_state='t')
            # models.Guide.objects.filter(pk=place_guide).update(guide_frequency='t')
            return work(request)
        else:
            return HttpResponseRedirect("/app01/work/")
    return HttpResponseRedirect("/app01/login/")


# 释放订单
def work_release(request):
    if request.method == 'POST':
        # place_room = 99
        # place_account = 99
        # place_guide = 99
        place_room = request.POST.get('room_id')
        place_account = request.POST.get('account_id')
        place_guide = request.POST.get('guide_id')
        print(place_room, place_account, place_guide)
        if place_room != '':
            models.Room.objects.filter(pk=place_room).update(room_state='f')
        if place_account != '':
            models.Account.objects.filter(pk=place_account).update(account_state='f')
        if place_guide != '':
            models.Guide.objects.filter(pk=place_guide).update(guide_state='f')
        return work(request)
    else:
        return HttpResponseRedirect("/app01/login/")
